package org.nuist.service.user;

import org.nuist.pojo.User;
import org.nuist.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository userRepository;

    @Override
    public void save(User user) {
        userRepository.save(user);
    }

    @Override
    public void delete(User user) {
        userRepository.delete(user);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public User findUserByName(String name) {
        return userRepository.findUserByName(name);
    }

    @Override
    public User findUserById(Integer id) {
        return userRepository.findUserById(id);
    }

    @Override
    public void modifyUser(User user) {
        int userId = user.getUserId();
        User oldUser = userRepository.findUserById(userId);
        oldUser.setUserName(user.getUserName());
        oldUser.setPassword(user.getPassword());
        oldUser.setPerms(user.getPerms());
        oldUser.setMajor(user.getMajor());
        oldUser.setStudentId(user.getStudentId());
        userRepository.save(oldUser);
    }



}
